package com.lottery.im.module.login.presenter;

import android.app.Activity;
import android.util.Log;
import com.lottery.im.api.ApiClient;
import com.lottery.im.api.observer.ShowTipObserver;
import com.lottery.im.api.result.ApiResult;
import com.lottery.im.api.result.entity.LoginEntity;
import com.lottery.im.base.BaseFragmentPresenter;
import com.lottery.im.module.login.view.LoinView;
import com.trello.rxlifecycle2.LifecycleProvider;
import com.trello.rxlifecycle2.android.FragmentEvent;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import java.util.concurrent.TimeUnit;

public class LoginPresenter extends BaseFragmentPresenter<LoinView> {

  public LoginPresenter(Activity activity,
      LoinView iView,
      LifecycleProvider<FragmentEvent> provider) {
    super(activity, iView, provider);
  }

  public void login(String account, String pwd) {

    ApiClient.getApiRetrofitInstance()
        .login(account, pwd, "Android")
        .throttleFirst(2, TimeUnit.SECONDS)
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .compose(provider.<ApiResult<LoginEntity>>bindToLifecycle())
        .subscribe(new ShowTipObserver<LoginEntity>(activity) {
          @Override public void onSuccess(LoginEntity loginEntity) {
            iView.loginSuccess(loginEntity);
          }
        });
  }
}
